var mysql = require('mysql');
var request = require('request');
var express = require('express');
var app = express();
var connection = mysql.createConnection({
  host: '127.0.0.1',
  port: '3306',
  user: 'root',
  password: 'admin',
  database: 'db_web'
})
connection.connect();

let httpUrl = "http://www.newsgd.com/news/2020-11/16/content_191721477.htm";
function req(url) {
  return new Promise(function (resolve, reject) {
    request.get(url, function (err, response, body) {
      if (err) {
        reject(err);
      } else {
        resolve({ response, body });
      }
    })
  })
}
var str3 = []
async function getClassUrl() {
  let { response, body } = await req(httpUrl);
  let reg = /<h2 class="title" id="title"> (.*?)<\/h2>/igs
  let reg1 = /<a href="http:\/\/">(.*?)<\/a>/igs
  let reg3 = /<p style="TEXT-ALIGN: center" align="center">.*?<\/font>(.*?)<!--\/enpcontent--><!--\/enpcontent-->/igs
  let result = reg.exec(body)[1];
  let result2 = reg1.exec(body)[1];
  let result3 = reg3.exec(body)[1];
  //console.log(result3);
  result3 = result3.replace(/(<p>|<\/p>|\n|\r|\t)/g, "");
  //result3 = result3.replace(/'/g, '’');
  result3 = result3.replace(/"/g, '^^');
  result3 = result3.replace(/'/g, '++');
  result3 = result3.replace(/-/g, '=');
  //console.log(result3);
  //result3 = result3.replace(/\s+/, ' ');
  
  //console.log(result3);
  var str1 = result3;
  str1 = str1.split(/\s+/);
  str1 = str1.filter(item => item)
  //console.log(str1);
  str1.forEach(
    function (e) {
      var str2 = e.split(/(\.|\,|\^\^|\+\+)/g);
      str2.forEach(w => {
        str3.push(w)
      })
    })
  str3 = str3.filter(item => item)
  str4 = JSON.stringify(str3);

  var obj = {
    Title: result,
    author: result2,
    content: str4
  }
  return obj;


}
//getClassUrl()
getClassUrl().then((obj) => {
  console.log(obj);
  console.log(str3);
  let insertSql = `INSERT INTO tb_message (Title, author, content) VALUES ('${obj.Title}','${obj.author}','${obj.content}');`
  connection.query(insertSql, function (err, result) {
    if (err)
      console.log('INSERT ERROR:' + err);
    console.log(result);
  });
  str3.forEach(function(word,index){
    var insertSql = `INSERT INTO tb_msg_word (id, english) VALUES ('${index}','${word}');`
    connection.query(insertSql, function (err, result) {
      if (err)
        console.log('INSERT ERROR:' + err);
      console.log(result);
    });
  })
})


// app.get('/',function(req, res) {
//   res.setHeader("Access-Control-Allow-Origin", "*");
//   res.send(msg);
// });
// app.listen(1001, function(){
//   console.log("Server running at 1001 port");
// })